package com.xm.service.impl;

import com.xm.entity.User;
import com.xm.mapper.UserMapper;
import com.xm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    User user = new User();

    @Override
    public boolean login(String username, String password) {
        User user = userMapper.findByUsername(username);
        return user != null && user.getPassword().equals(password);
    }

    @Override
    public boolean register(String username, String password) {
        if (userMapper.findByUsername(username) != null) {
            return false;
        }

        user.setUsername(username);
        user.setPassword(password);
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public int updateUser(User user) {
        if (userMapper.selectById(user.getId()) != null) {
            user.setUsername(user.getUsername());
            user.setPassword(user.getPassword());
            return userMapper.updateUser(user);
        } else {
            return 0;
        }
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }
}
